package file

import (
	"io"
	"lifi"
	"lifi/plugins/sources"
	"log"
	"os"
)

type File struct {
	FilePath string `json:"filePath,omitempty" yaml:"filePath,omitempty"`
}

func (f File) Init() error {
	return nil
}

func (f File) Gather(collector lifi.Collector) error {
	file, err := os.Open(f.FilePath)
	if err != nil {
		return err
	}
	defer file.Close()

	data, err := io.ReadAll(file)
	if err != nil {
		return err
	}
	log.Printf("gather data:", string(data))
	d := lifi.NewByteData(data, f.FilePath)
	collector.Collect(d)
	return nil

}

func (f File) Close() error {
	//TODO implement me
	panic("implement me")
}

var _ lifi.Source = &File{}

func init() {
	sources.Add("file", func() lifi.Source {
		return &File{}
	})
}
